package ma.mindware.mpm.util;



import java.io.IOException;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringBufferInputStream;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;

import javax.servlet.http.HttpSession;

import jpmapper.core.FOREIGN;
import jpmapper.core.ID;


import ma.mindware.met.core.technicalservice.DBService;
import ma.mindware.met.core.technicalservice.ServiceRunner;
import ma.mindware.met.core.technicalservice.TechnicalServiceException;
import ma.mindware.mpm.technicalservice.db.PagerDBService;

import org.apache.catalina.connector.Request;
import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.opensymphony.xwork2.ActionSupport;

public class Pager extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private InputStream data;
	private ServiceRunner service;
	private Object serviceParameters[];
	
	
	private String jsonPagerObject;
	private String nextAction;
	private String jspPagePath;
	private String modelPackage;
	private ArrayList<Object> listPagerObject;
	private ArrayList<Integer> listIndexPages;
	private Integer nbrPage;
	private Integer lengthListPagerObject;
	private Long currentPage;
	private Long listObjectLength;
	
	
	
	
	public Long getCurrentPagePager() {
		return currentPage;
	}

	public void setCurrentPagePager(Long currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getLengthListPagerObject() {
		return lengthListPagerObject;
	}

	public void setLengthListPagerObject(Integer lengthListPagerObject) {
		this.lengthListPagerObject = lengthListPagerObject;
	}

	public ArrayList<Integer> getListIndexPages() {
		return listIndexPages;
	}

	public void setListIndexPages(ArrayList<Integer> listIndexPages) {
		this.listIndexPages = listIndexPages;
	}

	public ArrayList<Object> getListPagerObject() {
		return listPagerObject;
	}

	public void setListPagerObject(ArrayList<Object> listPagerObject) {
		this.listPagerObject = listPagerObject;
	}

	public String getJsonPagerObject() {
		return jsonPagerObject;
	}

	public void setJsonPagerObject(String jsonPagerObject) {
		this.jsonPagerObject = jsonPagerObject;
	}

	private HttpSession session =ServletActionContext.getRequest().getSession();
	
	public InputStream getData() {
		return data;
	}
	

	public void setData(InputStream data) {
		this.data = data;
	}
	
	private Properties getConfig(){
		Properties config=((Properties)session.getServletContext().getAttribute("config"));
		return config;
	}
	
	private ServiceRunner getServiceRunner(){
		service= ServiceRunner.getInstance();
		serviceParameters=new Object[1]; 
		serviceParameters[0]=getConfig().getProperty("dbInfoFilePath");
		return service;
	}

	

	public String getNextAction() {
	    return nextAction;
	}
	
	@SuppressWarnings("unchecked")
	public String getTable(){
		this.getServiceRunner();
		try {
			JSONObject jsonObject=new JSONObject(jsonPagerObject);
			Long nbrRows=jsonObject.getLong("nbrRows");
			currentPage=jsonObject.getLong("currentPage");
			jspPagePath=jsonObject.getString("jspPagePath");
			modelPackage=jsonObject.getString("modelPackage");
			Long startListPager=currentPage * nbrRows;
			Long endListPager= nbrRows;
			String objectModel=jsonObject.getString("object");
			String jspPage=jsonObject.getString("jspPage");
			Class<?> className= Class.forName(modelPackage+"."+objectModel);
			//listPagerObject=(ArrayList<Object>) service.run(DBService.class, serviceParameters, "list", className.newInstance(),startListPager,endListPager);
			listPagerObject=(ArrayList<Object>) service.run(PagerDBService.class,serviceParameters,"list",className,startListPager,endListPager);
			this.nextAction=jspPagePath+jspPage;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TechnicalServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return "success";
	}
	@SuppressWarnings({ "unchecked", "deprecation" })
	public String getPager(){
		this.getServiceRunner();
		try {
			JSONObject jsonObject=new JSONObject(jsonPagerObject);
			Long nbrRows=jsonObject.getLong("nbrRows");
			String objectModel=jsonObject.getString("object");
			currentPage=jsonObject.getLong("currentPage");
			String jspPage=jsonObject.getString("jspPage");
			jspPagePath=jsonObject.getString("jspPagePath");
			modelPackage=jsonObject.getString("modelPackage");
			Class<?> className= Class.forName(modelPackage+"."+objectModel);
			//listPagerObject=(ArrayList<Object>) service.run(DBService.class, serviceParameters, "list", className.newInstance(),new Long(0),nbrRows);
			listPagerObject=(ArrayList<Object>) service.run(PagerDBService.class,serviceParameters,"list",className,new Long(0),nbrRows);
			listObjectLength=(Long) service.run(DBService.class, serviceParameters, "count",className.newInstance());
			this.nextAction=jspPagePath+jspPage;
			lengthListPagerObject=listPagerObject.size();
			if(listObjectLength==0){
				nbrPage=0;
			}
			else if(listObjectLength<=nbrRows){
				nbrPage=1;
			}else if ((listObjectLength % nbrRows)==0) {
				nbrPage=(int) (listObjectLength/nbrRows);
			}else{
				nbrPage=(int) (listObjectLength/nbrRows)+1;
			}
			
			jsonObject.put("nbrPage", nbrPage);
			data=new StringBufferInputStream(jsonObject.toString());
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TechnicalServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return "success";
	}
	
	
}